﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Xml;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TShooter.TfsWord.TeamFoundation
{
    internal sealed class TfsConnection : ITfsConnection
    {
        #region Constructors and member variables

        private const string NO_CREDENTIALS = "N/A";
        private const string STRING_FORMAT = "Server: {0}; Project: {1}; Credentials: {2}";
        private readonly ConnectionInfo _ConnectionInfo;
        private readonly ICredentials _Credentials;
        private TfsTeamProjectCollection _Server;

        internal TfsConnection(ConnectionInfo connectionInfo)
        {
            _ConnectionInfo = connectionInfo;
        }

        internal TfsConnection(ConnectionInfo connectionInfo, ICredentials credentials)
            : this(connectionInfo)
        {
            _Credentials = credentials;
        }

        #endregion

        #region Properties

        public string ServerUrl
        {
            get { return _ConnectionInfo.TeamProject; }
        }

        private TfsTeamProjectCollection Server
        {
            get
            {
                if (_Server == null)
                {
                    _Server = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
                                                                                          _ConnectionInfo.
                                                                                              TeamProjectCollectionUri,
                                                                                          new UICredentialsProvider());
                }
                _Server.EnsureAuthenticated();

                return _Server;
            }
        }

        #endregion

        #region ITfsConnection Members

        public string ServerName
        {
            get { throw new NotImplementedException(); }
        }

        public string ProjectName
        {
            get { return _ConnectionInfo.TeamProject; }
        }

        public event EventHandler<StatusChangedEventArgs> StatusChanged;

        public IEnumerable<string> GetProjects()
        {
            var css = Server.GetService<ICommonStructureService>();

            return from projectInfo in css.ListAllProjects()
                   where projectInfo.Status == ProjectState.WellFormed
                   select projectInfo.Name;
        }

        public IEnumerable<string> GetWorkItemTypeDefinitions()
        {
            var store = Server.GetService<WorkItemStore>();
            Project project = store.Projects[_ConnectionInfo.TeamProject];

            return from WorkItemType item in project.WorkItemTypes
                   select item.Name;
        }

        public string GetWorkItem(int selectedWorkItem)
        {
            var store = Server.GetService<WorkItemStore>();
            var workItem = store.GetWorkItem(selectedWorkItem);

            return null; // wi -> XML
        }

        public string GetBlankWorkItem(string workItemTypeDefinition)
        {
            throw new NotImplementedException();
        }

        public string GetWorkItemTypeDefinition(string witdName)
        {
            var store = Server.GetService<WorkItemStore>();
            Project project = store.Projects[_ConnectionInfo.TeamProject];

            //store.GetWorkItem()
            XmlDocument doc = project.WorkItemTypes[witdName].Export(true);

            return doc.OuterXml;
        }

        #endregion

        public override string ToString()
        {
            return string.Format(STRING_FORMAT,
                                 _ConnectionInfo.TeamProjectCollectionUri,
                                 _ConnectionInfo.TeamProject,
                                 _Credentials == null ? NO_CREDENTIALS : _Credentials.ToString());
        }
    }
}